﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Toolkit.Web.Infastructure;

namespace Toolkit.Web.ViewComponents.Panel
{
    public sealed class PanelHtmlBuilder : ViewComponentHtmlBuilderBase
    {
        public PanelHtmlBuilder(Panel panel)
            : base(panel)
        {
            if (panel == null)
            {
                throw new ArgumentNullException("panel");
            }

            _panel = panel;
            CssClassNames = new ComponentCssClassNames();
        }

        private Panel _panel
        {
            get;
            set;
        }

        public ComponentCssClassNames CssClassNames
        {
            get;
            private set;
        }

        public override IHtmlNode Html()
        {
            return PanelMarkup();
        }

        public IHtmlNode PanelMarkup()
        {
            IHtmlNode htmlTag = new HtmlNode("div");
            htmlTag.MergeAttributes(_panel.HtmlAttributes, true);
            htmlTag.AddClass(CssClassNames.ComponentWrapper);

            if (_panel.Collapsible)
            {
                htmlTag.AddClass(CssClassNames.Collapsible);
            }

            if (_panel.Collapsible && _panel.CollapseOnRender)
            {
                htmlTag.AddClass(CssClassNames.Collapsed);
            }

            if (_panel.ClientEventHandlers != null)
            {
                if (!string.IsNullOrEmpty(_panel.ClientEventHandlers.OnBeforeCollapse))
                {
                    htmlTag.Attributes["data-before-collapse"] = _panel.ClientEventHandlers.OnBeforeCollapse;
                }

                if (!string.IsNullOrEmpty(_panel.ClientEventHandlers.OnBeforeExpand))
                {
                    htmlTag.Attributes["data-before-expand"] = _panel.ClientEventHandlers.OnBeforeExpand;
                }
            }

            htmlTag.Append(HeaderMarkup());
            htmlTag.Append(BodyMarkup());

            return htmlTag;
        }

        public IHtmlNode HeaderMarkup()
        {
            IHtmlNode htmlTag = new HtmlNode("div");
            htmlTag.AddClass(CssClassNames.Header);

            if (_panel.Collapsible)
            {
                htmlTag.Append(CollapsibleMarkup());
            }

            if (_panel.SecondaryHeader != null && _panel.SecondaryHeader.HasValue())
            {
                htmlTag.Append(SecondaryHeaderMarkup());
            }

            htmlTag.Append(PrimaryHeaderMarkup());

            return htmlTag;
        }

        public IHtmlNode CollapsibleMarkup()
        {
            IHtmlNode htmlTag = new HtmlNode("div");
            htmlTag.AddClass(CssClassNames.CollapsibleIcon);

            IHtmlNode iconTag = new HtmlNode("span");
            iconTag.AddClass("vc-icon");
            htmlTag.Append(iconTag);

            return htmlTag;
        }

        public IHtmlNode PrimaryHeaderMarkup()
        {
            IHtmlNode htmlTag = new HtmlNode("div");
            htmlTag.AddClass(CssClassNames.PrimaryHeader);

            if (_panel.PrimaryHeader != null && _panel.PrimaryHeader.HasValue())
            {
                htmlTag.ApplyTemplate(_panel.PrimaryHeader);
            }

            return htmlTag;
        }

        public IHtmlNode SecondaryHeaderMarkup()
        {
            IHtmlNode htmlTag = new HtmlNode("div");
            htmlTag.AddClass(CssClassNames.SecondaryHeader);

            if (_panel.SecondaryHeader != null && _panel.SecondaryHeader.HasValue())
            {
                htmlTag.ApplyTemplate(_panel.SecondaryHeader);
            }

            return htmlTag;
        }

        public IHtmlNode BodyMarkup()
        {
            IHtmlNode htmlTag = new HtmlNode("div");

            if (_panel.BodyHtmlAttributes != null)
            {
                htmlTag.MergeAttributes(_panel.BodyHtmlAttributes, true);
            }

            htmlTag.AddClass(CssClassNames.BodyWrapper);

            if (_panel.Collapsible && _panel.CollapseOnRender)
            {
                htmlTag.AddStyle("display", "none");
            }

            IHtmlNode bodyTag = new HtmlNode("div");
            bodyTag.AddClass(CssClassNames.Body);

            if (_panel.Body != null && _panel.Body.HasValue())
            {
                bodyTag.ApplyTemplate(_panel.Body);
            }

            htmlTag.Append(bodyTag);

            return htmlTag;
        }

        public class ComponentCssClassNames
        {
            public readonly string ComponentWrapper = "vc-pnl";
            public readonly string Header = "vc-pnl-header";
            public readonly string Body = "vc-pnl-body";
            public readonly string BodyWrapper = "vc-pnl-bodyWrap";
            public readonly string PrimaryHeader = "vc-pnl-primary";
            public readonly string SecondaryHeader = "vc-pnl-secondary";
            public readonly string Collapsible = "vc-pnl-collapsible";
            public readonly string CollapsibleIcon = "vc-pnl-iconWrap";
            public readonly string Collapsed = "vc-pnl-collapsed";
        }
    }
}
